

using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Linq;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;
using Abp.Domain.Services;

using Yozeev;
using Yozeev.BusinessLogic;
using Microsoft.AspNetCore.Identity;
using Abp.Authorization.Users;
using System.Runtime.CompilerServices;

namespace Yozeev.BusinessLogic.DomainService
{
    /// <summary>
    /// Customer领域层的业务管理
    ///</summary>
    public class CustomerManager :YozeevDomainServiceBase, ICustomerManager
    {
		
		private readonly IRepository<Customer,Guid> _repository;

		/// <summary>
		/// Customer的构造方法
		///</summary>
		public CustomerManager(
			IRepository<Customer, Guid> repository
		)
		{
			_repository =  repository;
		}


		/// <summary>
		/// 初始化
		///</summary>
		public void InitCustomer()
		{
			throw new NotImplementedException();
		}

        /// <summary>
        /// 根据证件号，
        /// </summary>
        public async Task<Customer> GetByIdentityNo(String identity)
        {
			Customer customer = await _repository.FirstOrDefaultAsync(c => c.CertificatesNum == identity);
            return customer;
        }
		/// <summary>
		/// 根据游客Id列表查询
		/// </summary>
		/// <param name="guids"></param>
		/// <returns></returns>
		public async Task<List<Customer>> GetCustomersByIds(Guid[] guids)
		{
			var list = await _repository.GetAll().Where(m => guids.Contains(m.Id)).ToListAsync();
			return list;
		}

		/// <summary>
		/// 根据Id查询
		/// </summary>
		public async Task<Customer> GetCustomerById(Guid Id)
		{
			Customer customer = await _repository.FirstOrDefaultAsync(c => c.Id == Id);
			return customer;
		}


		/// <summary>
		/// 验证密码是否正确
		/// </summary>
		/// <param name="emailConfirmationCode"></param>
		/// <returns></returns>
		public async Task<Customer> GetPwdConfirmationCode(string PwdConfirmationCode)
		{
			var custom = _repository.GetAll().FirstOrDefault(o => o.LoginPwd == PwdConfirmationCode);
			if (custom.LoginPwd == null)
			{
				throw new UserFriendlyException("密码不可为空");
			}
			return custom;
		}

		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<Customer> CreateAsync(Customer entity)
		{
			entity.Id = await _repository.InsertAndGetIdAsync(entity);
			return entity;
		}

		/// <summary>
		/// 密码校验
		/// </summary>
		/// <param name="Name"></param>
		/// <param name="Password"></param>
		/// <returns></returns>
		public async Task<Customer> CheckNamePassword(string Name,string Password)
		{
			var customer = _repository.GetAll().FirstOrDefault(o => o.CustomerName == Name);
			if (customer == null)
			{
				throw new UserFriendlyException("该用户不存在");
			}
			if(customer.LoginPwd != Password)
			{
				throw new UserFriendlyException("密码不正确");
			}
			return customer;
		}
		/// <summary>
		/// 保存游客信息，非异步
		/// </summary>
		/// <param name="customer"></param>
		/// <returns></returns>
		public Customer SaveEntity(Customer customer)
		{
			var entity = _repository.FirstOrDefault(m => m.CertificatesNum == customer.CertificatesNum);
			if(entity!=null)
			{
				entity.CustomerName = customer.CustomerName;
				entity.Mobile = customer.Mobile;
				entity.Sex = customer.Sex;
				entity.VerifiableType = customer.VerifiableType;
				_repository.Update(entity);
			}
			else
			{
				entity = _repository.Insert(customer);
			}
			CurrentUnitOfWork.SaveChanges();
			return entity;
		}

	}
}
